package com.jt.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jt.mapper.OrderItemMapper;
import com.jt.mapper.OrderMapper;
import com.jt.mapper.OrderShippingMapper;
import com.jt.pojo.Order;
import com.jt.pojo.OrderItem;
import com.jt.pojo.OrderShipping;

@Service
public class OrderServiceImpl implements DubboOrderService {
	
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderShippingMapper orderShippingMapper;
	@Autowired
	private OrderItemMapper orderItemMapper;

	@Transactional
	@Override
	public String saveOrder(Order order) {
		//orderId由登录用户id+当前时间戳手动拼接
		String orderId = ""+order.getUserId()+System.currentTimeMillis();
		//完成订单入库操作
		order.setOrderId(orderId).setStatus(1);
		orderMapper.insert(order);
		System.out.println("订单入库成功！！");
		//完成订单商品入库
		List<OrderItem>orderItems = order.getOrderItems();
		for (OrderItem orderIteme: orderItems) {
			orderIteme.setOrderId(orderId);
			orderItemMapper.insert(orderIteme);
		}
		System.out.println("订单商品入库成功！！");
		//完成订单物流入库
		OrderShipping orderShipping = order.getOrderShipping();
		orderShipping.setOrderId(orderId);
		orderShippingMapper.insert(orderShipping);
		System.out.println("订单物流入库成功!!");
		return orderId;
	}

	@Override
	public Order findOrderById(String id) {
		//查询order信息
		Order order = orderMapper.selectById(id);
		//查询订单物流
		OrderShipping orderShipping = orderShippingMapper.selectById(id);
		//查询订单商品
		QueryWrapper<OrderItem>queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("order_id",id);
		List<OrderItem>orderItems = orderItemMapper.selectList(queryWrapper);
		//为订单模块赋值
		order.setOrderItems(orderItems).setOrderShipping(orderShipping);
		return order;
	}
}
